package edu.bmstu.rslab;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;

/**
 * Created by Chikh (using IntelliJ IDEA).
* Date: 04.05.2009
* Time: 20:25:35
*/
class SetRobotPositionAction extends AbstractAction {
    private final CoordinateSystemPanel coordinateSystemPanel;
    private final MobileRobot robot;
    private final Action selectPositionMaskAction;
    private final VideoPanel videoPanel;

    public SetRobotPositionAction(CoordinateSystemPanel coordinateSystemPanel, MobileRobot robot,
                                  Action selectPositionMaskAction, VideoPanel videoPanel) {
        super("Set robot position");
        this.coordinateSystemPanel = coordinateSystemPanel;
        this.robot = robot;
        this.selectPositionMaskAction = selectPositionMaskAction;
        this.videoPanel = videoPanel;
        setEnabled(false);
    }

    public void actionPerformed(ActionEvent e) {
        setEnabled(false);

        coordinateSystemPanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                Random generator = new Random();
                robot.setPosition(coordinateSystemPanel.getScaledPoint(e.getPoint()));
                robot.setDirection(generator.nextDouble()*2*Math.PI);
                videoPanel.positionSet();
                selectPositionMaskAction.setEnabled(true);
                coordinateSystemPanel.removeMouseListener(this);
            }
        });
    }
}
